package com.example.demo.cotroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.config.AjaxResult;
import com.example.demo.pojo.SysRole;
import com.example.demo.pojo.SysUser;
import com.example.demo.service.SysRoleService;
import com.example.demo.uilt.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Api
@ApiModel("管理员")
@Controller
public class SysRoleCotroller {
  @Autowired
  private SysRoleService sysRoleService;
  @PostMapping("list")
  @ResponseBody
  @ApiOperation("集合")
  public AjaxResult roleList(@RequestBody SysRole sysRole, Integer pageIndex, Integer pageSize){
    return sysRoleService.roleLists(sysRole, pageIndex, pageSize);
  }
  @GetMapping("roleDelete")
  @PreAuthorize("hasAuthority('system:role:remove')")
  @ResponseBody
  @ApiOperation("删除角色")
  public AjaxResult roleDelete(@RequestBody SysRole sysRole){
    Long roleId = sysRole.getRoleId();
    QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();
    queryWrapper.eq("role_id",roleId);
    boolean remove = sysRoleService.remove(queryWrapper);
    if(remove){
      return AjaxResult.success("成功",roleId);
    }else {
      return AjaxResult.error("失败",roleId);
    }
  }
  @ResponseBody
  @ApiOperation("新增")
  @GetMapping("/roleInsert")
  public ResponseResult roleInsert(@RequestBody SysRole sysRole){
    return sysRoleService.roleInsert(sysRole);
  }
  @ResponseBody
  @ApiOperation("修改")
  @GetMapping("/roleUpdate")
  public ResponseResult roleUpdate(@RequestBody SysRole sysRole) {
    return sysRoleService.roleUpdate(sysRole);
  }
}
